/* $RCSFile: ADNDynBulkoperation.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynBulkoperation.cs $
 * $log$
 * Revision 54 2010/10/03 22:26:39  christian.surieux
 *   Added test to avoid create on entities which can't be created
 * Revision 19 2010/10/02 14:01:48  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:32  christian.surieux
 *   Added Log to All cs files
 */

/* Copyrights Christian Surieux 2010 */
/* Class ADNDynBulkoperation */
/* Date 2010-01-06 16:11:40Z */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using CrmSdk;

namespace AddonNice.CRMWrapper
{
    /// <summary>
    /// Quick campaign
    /// </summary>
    public class ADNDynBulkoperation : DynEntity
    {
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.bulkoperation)
            {}

            public override Type DynType
            { get { return typeof(ADNDynBulkoperation); } }

            public override string DynTypename
            { get { return CrmStr.bulkoperation; } }

            public override DynEntity Build()
            {   return new ADNDynBulkoperation();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynBulkoperation(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynBulkoperation(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynBulkoperation(w,PrimaryKeyValue);}
        }

        static ADNDynBulkoperation(){new Ftor();} 
        
        public override bool HasOwningBusinessUnitProperty
        { get { return true; } }
        
        public ADNDynBulkoperation() : base() { }
        
        public ADNDynBulkoperation(CRMWrapperBase w,string PrimaryFieldValue)
            : base(w,CrmStr.bulkoperation)
        {
            if ( PrimaryFieldValue != string.Empty )
                subject = PrimaryFieldValue;
        }
        
        public ADNDynBulkoperation(CRMWrapperBase w,Guid CrmId) : base(w,CrmStr.bulkoperation,CrmId) { }
        
        public ADNDynBulkoperation(CRMWrapperBase w,InternalDynamicEntity e) : base(w,CrmStr.bulkoperation,e,null,null) { }
        
        public ADNDynBulkoperation(CRMWrapperBase w,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s) : base(w,CrmStr.bulkoperation,e,prop,s) { }
        
        public ADNDynBulkoperation(ResetWrapperHandler fn,string PrimaryFieldValue)
            : base(fn,CrmStr.bulkoperation)
        {
            if ( PrimaryFieldValue != string.Empty )
                subject = PrimaryFieldValue;
        }
        
        public ADNDynBulkoperation(ResetWrapperHandler fn,Guid CrmId) : base(fn,CrmStr.bulkoperation,CrmId) { }
        
        public ADNDynBulkoperation(ResetWrapperHandler fn,InternalDynamicEntity e) : base(fn,CrmStr.bulkoperation,e,null,null) { }
        
        public ADNDynBulkoperation(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s) : base(fn,CrmStr.bulkoperation,e,prop,s) { }

        /* Properties */

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public Guid activityid
        {
            get{ return KeyPropertyValue(CrmStr.activityid); }
            set{ AddKeyProperty(CrmStr.activityid,value); }
        }

        protected override void SetCrmIdProperty(Guid crmId)
        {
            if ( !dicProperty.ContainsKey(CrmStr.activityid) || KeyPropertyValue(CrmStr.activityid) != crmId )
                activityid = crmId;
        }
        public override Guid PrimaryKeyGuidValue
        {
            get{ return activityid; }
            set { activityid = value; }
        }
        
        public override bool CanCreate
        {
           get
           {
               return false;
           }
        }
        
        public override string PrimaryFieldName
        {
            get{ return CrmStr.subject; }
            set{}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return subject;
            }
            set
            {
                subject =   value;
            }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int actualdurationminutes
        {
            get{ return NumberPropertyValue(CrmStr.actualdurationminutes); }
            set{ AddNumberProperty(CrmStr.actualdurationminutes,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string actualend
        {
            get{ return DateTimePropertyValue(CrmStr.actualend); }
            set{ AddDateTimeProperty(CrmStr.actualend,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string actualstart
        {
            get{ return DateTimePropertyValue(CrmStr.actualstart); }
            set{ AddDateTimeProperty(CrmStr.actualstart,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string bulkoperationnumber
        {
            get{ return StringPropertyValue(CrmStr.bulkoperationnumber); }
            set{ AddStringProperty(CrmStr.bulkoperationnumber,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int createdrecordtypecode
        {
            get{ return PicklistPropertyIntValue(CrmStr.createdrecordtypecode); }
            set{ AddPicklistProperty(CrmStr.createdrecordtypecode,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string createdrecordtypecodeLabel
        {
            get{ return PicklistPropertyLabelValue(CrmStr.createdrecordtypecode); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string description
        {
            get{ return StringPropertyValue(CrmStr.description); }
            set{ AddStringProperty(CrmStr.description,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int errornumber
        {
            get{ return NumberPropertyValue(CrmStr.errornumber); }
            set{ AddNumberProperty(CrmStr.errornumber,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int failurecount
        {
            get{ return NumberPropertyValue(CrmStr.failurecount); }
            set{ AddNumberProperty(CrmStr.failurecount,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public bool isbilled
        {
            get{ return BooleanPropertyValue(CrmStr.isbilled); }
            set{ AddBooleanProperty(CrmStr.isbilled,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public bool isworkflowcreated
        {
            get{ return BooleanPropertyValue(CrmStr.isworkflowcreated); }
            set{ AddBooleanProperty(CrmStr.isworkflowcreated,value); }
        }

	    /// <summary>
	    /// Bulk operation types.
	    /// </summary>
	    public enum BulkOperationType : int
	    {
		    CreateActivities        =   2,
		    SendDirectMail          =   3,
		    Propagate               =   4,
		    Execute                 =   5,
		    MiniCampaign            =   7
	    }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int operationtypecode
        {
            get{ return PicklistPropertyIntValue(CrmStr.operationtypecode); }
            set{ AddPicklistProperty(CrmStr.operationtypecode,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string operationtypecodeLabel
        {
            get{ return PicklistPropertyLabelValue(CrmStr.operationtypecode); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public Guid owningbusinessunit
        {
            get{ return LookupPropertyValue(CrmStr.owningbusinessunit); }
            set{ AddLookupProperty(CrmStr.owningbusinessunit,CrmStr.businessunit,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public Guid owninguser
        {
            get{ return LookupPropertyValue(CrmStr.owninguser); }
            set{ AddLookupProperty(CrmStr.owninguser,CrmStr.systemuser,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string parameters
        {
            get{ return StringPropertyValue(CrmStr.parameters); }
            set{ AddStringProperty(CrmStr.parameters,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public Guid regardingobjectid
        {
            get{ return LookupPropertyValue(CrmStr.regardingobjectid); }
            set{ AddLookupProperty(CrmStr.regardingobjectid,DefaultRegardingObjectName,value); }
        }

        public string regardingobjectName
        {
            get{ return LookupPropertyName(CrmStr.regardingobjectid); }
        }

        public string regardingobjectType
        {
            get{ return LookupPropertyType(CrmStr.regardingobjectid); }
        }

        string _DefaultRegardingObjectName   =   CrmStr.account;
		public string DefaultRegardingObjectName
		{
			get
			{
				return _DefaultRegardingObjectName;
			}
			set
			{
                switch (value)
                {
                    case CrmStr.account:
                    case CrmStr.contact:
                    case CrmStr.bulkoperation:
                    case CrmStr.campaign: // not campaignactivity, sdk err
                    case CrmStr.incident:
                    case CrmStr.contract:
                    case CrmStr.invoice:
                    case CrmStr.lead:
                    case CrmStr.opportunity:
                    case CrmStr.salesorder:
                    case CrmStr.quote:
                        _DefaultRegardingObjectName  =   value;
                        break;
                }
			}
		}
        
        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int scheduleddurationminutes
        {
            get{ return NumberPropertyValue(CrmStr.scheduleddurationminutes); }
            set{ AddNumberProperty(CrmStr.scheduleddurationminutes,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string scheduledend
        {
            get{ return DateTimePropertyValue(CrmStr.scheduledend); }
            set{ AddDateTimeProperty(CrmStr.scheduledend,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string scheduledstart
        {
            get{ return DateTimePropertyValue(CrmStr.scheduledstart); }
            set{ AddDateTimeProperty(CrmStr.scheduledstart,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public Guid serviceid
        {
            get{ return LookupPropertyValue(CrmStr.serviceid); }
            set{ AddLookupProperty(CrmStr.serviceid,CrmStr.service,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string subject
        {
            get{ return StringPropertyValue(CrmStr.subject); }
            set{ AddStringProperty(CrmStr.subject,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int successcount
        {
            get{ return NumberPropertyValue(CrmStr.successcount); }
            set{ AddNumberProperty(CrmStr.successcount,value); }
        }
        /// <summary>
        /// Account
        /// Contact
        /// Lead
        /// </summary>
        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int targetedrecordtypecode
        {
            get{ return PicklistPropertyIntValue(CrmStr.targetedrecordtypecode); }
            set{ AddPicklistProperty(CrmStr.targetedrecordtypecode,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public string targetedrecordtypecodeLabel
        {
            get{ return PicklistPropertyLabelValue(CrmStr.targetedrecordtypecode); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int targetmemberscount
        {
            get{ return NumberPropertyValue(CrmStr.targetmemberscount); }
            set{ AddNumberProperty(CrmStr.targetmemberscount,value); }
        }

        [CrmDynAttribute(CrmStr.bulkoperation)]
        public int timezoneruleversionnumber
        {
            get{ return NumberPropertyValue(CrmStr.timezoneruleversionnumber); }
            set{ AddNumberProperty(CrmStr.timezoneruleversionnumber,value); }
        }
 
        #region utilities 

        public enum BulkOperationSource : int
        {
            QuickCampaign       =   0,// Indicates that the bulk operation is for distributing a quick campaign to members of a list. 
            CampaignActivity    =   1 // Indicates that the bulk operation is for distributing a campaign activity to members of a list. 
        }



        public enum EntitySource : int
        {
            Account             =   0, // Specifies to retrieve account entities. 
            Contact             =   1, // Specifies to retrieve contact entities. 
            Lead                =   2, // Specifies to retrieve lead entities. 
            All                 =   3 // Specifies to retrieve all entities. 
        }

 	    public enum BulkOperationStatus : int
	    {
		    Pending                 =   1,
		    InProgress              =   2,
		    Aborted                 =   3,
		    Completed               =   4,
		    Cancelled               =   5
	    }

        public bool Wait4BulkCompletion(bool isAdmin,int seconds)
        {
            if (isAdmin )
                    adminRetrieve();
            else Retrieve();
            int secondsTicker = seconds;
            while (secondsTicker > 0 && statuscode != (int)BulkOperationStatus.Completed )
            {
                System.Threading.Thread.Sleep(1000);
                secondsTicker--;
                if (isAdmin )
                     adminRetrieve();
                else Retrieve();
            }
            return ( statuscode == (int)BulkOperationStatus.Completed);
        }
        
        #endregion utilities 
   
    }
}